package com.ppt.javappt;

import com.ppt.javappt.em.CssPathEnum;
import com.ppt.javappt.em.IconPathEnum;
import com.ppt.javappt.util.StringUtil;
import com.ppt.javappt.util.SystemUtil;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.scene.image.Image;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Objects;

/**
 * @author cys
 */
public class HelloApplication extends Application {
    public static Stage MainWindow;
    public static Node focus;
    public static Cursor cursor;
    // 切换视图的方法
    public static void changeView(String url, int x,int y){
        Parent root;
        try {
            root = FXMLLoader.load(HelloApplication.class.getResource(url));
            if(root!=null){
                Scene scene = new Scene(root);
                MainWindow.setWidth(x);
                MainWindow.setHeight(y);
                MainWindow.setScene(scene);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 添加子窗口的方法
    public static Stage addView(String url,int x,int y,String title){
        Stage stage = new Stage();
        Parent root;
        try{
            root = FXMLLoader.load(HelloApplication.class.getResource(url));
            if(root!=null){
                Scene scene = new Scene(root);
                stage.setScene(scene);
                stage.initOwner(HelloApplication.MainWindow);
                stage.initModality(Modality.APPLICATION_MODAL);
                stage.setTitle(title);
                stage.setHeight(y);
                stage.setWidth(x);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        stage.show();
        return stage;
    }

    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 1080, 720);
        // 当前焦点对象
        scene.focusOwnerProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == null) {return;}
            System.out.println(newValue);
            focus = newValue;
            String type = newValue.getTypeSelector();
            if (Objects.equals(type, "TextBox") ||
                SystemUtil.isDescendant(scene.lookup("#textSetting"), newValue)
            ){
                scene.lookup("#textSetting").setDisable(false);
            }
            else if (StringUtil.DISABLE_TEXT_SETTING_TYPES.contains(type)) {
                scene.lookup("#textSetting").setDisable(true);
            }
//            else if (Objects.equals(newValue.getTypeSelector(), "ComboBox")) {}
        });
        MainWindow = stage;
        stage.setTitle("JavaPPT");
        stage.setScene(scene);
        scene.getStylesheets().add(CssPathEnum.BUTTON_CSS.getUrl());
        scene.getStylesheets().add(CssPathEnum.TAB_CSS.getUrl());
        scene.getStylesheets().add(CssPathEnum.TEXTBOX_CSS.getUrl());

        cursor = new ImageCursor(new Image(IconPathEnum.CURSOR.getUrl(),32,32,false,true));
        scene.setCursor(cursor);
        stage.getIcons().add(new Image(IconPathEnum.PPT.getUrl()));

        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}